-- @owner: @zhanghuan96
-- @date: 2024/12/18
-- @testpoint: gms_utility.format_error_backtrace调用深度测试
@conn PrimaryDbAmode;
--前置条件
drop extension if exists gms_utility;
create extension gms_utility;
--step1:创建自定义函数;expect:成功
drop function if exists func_format0010_01;
create or replace function func_format0010_01(a int, b int)
returns int as $$
declare
    res int := 0;
begin
    res = a / b;
    return res;
exception
    when others then
    raise exception 'expected exception';
end;
$$ language plpgsql;
/
--step2:创建自定义函数，函数嵌套上述函数;expect:代码发生异常时，使用gms_utility.format_error_backtrace函数
drop function if exists func_format0010_02;
create or replace function func_format0010_02(a int, b int)
returns int as $$
declare
    res int := 0;
begin
    res := func_format0010_01(a, b);
    return res;
exception
    when others then
     RAISE INFO 'INFO:%', gms_utility.format_error_backtrace();
    return -1;
end;
$$ language plpgsql;
/
--step3:创建存储过程;expect:成功
drop procedure if exists proc_format0010;
create or replace procedure proc_format0010(count in out int, max in int)
as
begin
    if count < max then
        count := count + 1;
        proc_format0010(count, max);
    else
        func_format0010_01(100, 0);
    end if;
exception
    when others then
    RAISE INFO 'INFO:%', gms_utility.format_error_backtrace();
end;
/
--step4:执行存储过程;expect:成功
declare
    c int := 0;
begin
    proc_format0010(c, 5);
end;
/
--step5:清理环境;expect:成功
drop function if exists func_format0010_01;
drop function if exists func_format0010_02;
drop procedure if exists proc_format0010;